      1  import java.util.ArrayList;

      2  import javax.xml.parsers.DocumentBuilder;

      3  import javax.xml.parsers.DocumentBuilderFactory;

      4  import javax.xml.parsers.ParserConfigurationException;

      5  import org.w3c.dom.Document;

      6  import org.w3c.dom.Element;

      7  import org.w3c.dom.Text;

      8

      9  /**

     10     Builds a DOM document for an array list of items.

     11  */

     12  public class ItemListBuilder

     13  {

     14     private DocumentBuilder builder;

     15     private Document doc;

     16

     17     /**

     18        Constructs an item list builder.

     19     */

     20     public ItemListBuilder()

     21           throws ParserConfigurationException

     22     {

     23        DocumentBuilderFactory factory

     24              = DocumentBuilderFactory.newInstance();

     25        builder = factory.newDocumentBuilder();

     26     }

     27

     28     /**

     29        Builds a DOM document for an array list of items.

     30        @param items the items

     31        @return a DOM document describing the items

     32     */

     33     public Document build(ArrayList<LineItem> items)

     34     {

     35        doc = builder.newDocument();

     36        doc.appendChild(createItems(items));

     37        return doc;

     38     }

     39

     40     /**

     41        Builds a DOM element for an array list of items.

     42        @param items the items

     43        @return a DOM element describing the items

     44     */

     45     private Element createItems(ArrayList<LineItem> items)

     46     {

     47        Element e = doc.createElement("items");

     48

     49        for (LineItem anItem : items)

     50        {

     51           e.appendChild(createItem(anItem));

     52        }

     53

     54        return e;

     55     }

     56

     57     /**

     58        Builds a DOM element for an item.

     59        @param anItem the item

     60        @return a DOM element describing the item

     61     */

     62     private Element createItem(LineItem anItem)

     63     {

     64        Element e = doc.createElement("item");

     65

     66        e.appendChild(createProduct(anItem.getProduct()));

     67        e.appendChild(createTextElement(

     68              "quantity", "" + anItem.getQuantity()));

     69

     70        return e;

     71     }

     72

     73     /**

     74        Builds a DOM element for a product.

     75        @param p the product

     76        @return a DOM element describing the product

     77     */

     78     private Element createProduct(Product p)

     79     {

     80        Element e = doc.createElement("product");

     81

     82        e.appendChild(createTextElement(

     83              "description", p.getDescription()));

     84        e.appendChild(createTextElement(

     85              "price", "" + p.getPrice()));

     86

     87        return e;

     88     }

     89

     90     private Element createTextElement(String name, String text)

     91     {

     92        Text t = doc.createTextNode(text);

     93        Element e = doc.createElement(name);

     94        e.appendChild(t);

     95        return e;

     96     }

     97  }